package com.example.app.pages.main.home.accountList.viewModel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.example.app.data.bean.BaseResponseData
import com.example.app.data.bean.routeAccount.AccountRecord
import com.example.app.data.bean.routeAccount.AccountRecordPageData
import com.example.app.network.api.AccountService
import com.example.app.viewmodel.MainViewModel
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import retrofit2.HttpException

class AccountListViewModel(var mainViewModel: MainViewModel) : ViewModel() {
    // 在ViewModel中
    val pagingDataFlow = Pager(
        config = PagingConfig(pageSize = 20),
        pagingSourceFactory = {
            AccountListPagingSource(this)
        }
    ).flow.cachedIn(viewModelScope)
    //页面在上拉滚动的时候，记录index和offset
    var _firstItemIndexAndOffset = MutableStateFlow<Pair<Int, Int>?>(null)
    var firstItemIndexAndOffset = _firstItemIndexAndOffset.asStateFlow()
    fun flowUpdate_firstItemIndexAndOffset(value: Pair<Int, Int>) {
        _firstItemIndexAndOffset.update {
            value
        }
    }
    //顶部任务栏字体颜色
    var _refreshing = MutableStateFlow(false)
    var refreshing = _refreshing.asStateFlow()
    fun flowUpdate_refreshing(value: Boolean) {
        _refreshing.update { value }
    }

    var _accountList = MutableStateFlow(ArrayList<AccountRecord>())
    var accountList = _accountList.asStateFlow()
    fun flowUpdate_accountList(value: List<AccountRecord>) {
        _accountList.update {
            value.toMutableList() as ArrayList<AccountRecord>
        }
    }
    //topbar的颜色状态改变
    var _topBarAlpha = MutableStateFlow<Float>(0f)
    var topBarAlpha = _topBarAlpha.asStateFlow()
    fun flowUpdate_topBarAlpha(value: Float) {
        _topBarAlpha.update {
            value
        }
    }

    fun request_getAccountRecord(
            CreatorId: String? = null,
            AccountRelationPeopleId: String? = null,
            AccountType: String? = null,
            w: String? = null,
            getAccountRecordType: String? = null,
            pageNum: Int,
            pageSize: Int,
            dateRange: List<String>?,
    ): Deferred<BaseResponseData<AccountRecordPageData<AccountRecord>>> {
        return viewModelScope.async {
            try {
                var result = viewModelScope.async {
                    AccountService.service.getAccountRecord(
                        CreatorId,
                        AccountRelationPeopleId,
                        AccountType,
                        w,
                        getAccountRecordType,
                        pageNum,
                        pageSize,
                        dateRange,
                    )
                }.await()

                mainViewModel.requestHandler(result)
                return@async result
            } catch (e: HttpException) {
                var result = viewModelScope.async {
                    BaseResponseData<AccountRecordPageData<AccountRecord>>(
                        code = e.code(),
                        data = null,
                        msg = e.message.toString()
                    )
                }.await()
                mainViewModel.requestHandler(result)
                return@async result
            }
        }
    }
}